<?php
if( !defined('IN_INDEX') )
{
	die('This page is hidden.');
}
if(!checkSession())
{
			function redirect($url)
			{
			?>
				<script type="text/javascript">
					{
					window.location = "<?=$url?>";
					}
				</script>
			<?php 
			}
			redirect("index.php?action=login");
	exit;
}

$friend_status = "";
if(isset($_GET['accept']))
{
	$sql = "UPDATE user_friends SET friend_status='accepted', friend_time='" . time() . "' WHERE friend_id='" . $_GET['accept'] . "'";
	
	if(!$result = $db->sql_query($sql))
	{
		printerror("SQL ERROR", mysql_error() ."<br />" . mysql_errno());
	}
	else
	{
		echo"<br /><br /><br /><br /><br /><br /><br /><br /><br />
				<center><meta http-equiv=\"refresh\"content=\"2;url=index.php?action=profile&amp;sub=friends\">";
				echo "<br/><h2>" . ucfirst(translate('friend_accepted')) . "</h2>\n";
				echo "<br/><br />" . translate('redirectedin'). " <a href=\"index.php?action=profile&amp;sub=friends\">" . translate('clickhere') . "</a>.</center><div class='fixed'>
							</div>
						</div>
						</div>
					<div class='fixed'>
					</div>
					<div id='footer'>
					<a id='gotop' href='#' onclick='MGJS.goTop();return false;'>" . translate('top') . "</a>
						<div align='center' id='copyright'>&copy; " . translate('copyrightyear') . " " . translate('header_text'). " - " .  translate('curversion') ." - <a href='index.php?action=contact'>" . translate('contact') ."</a> - <a href='index.php?action=about'>" . translate('about') . "</a> - " . translate('onlineusers'). ": " . getActiveUsers() . "<br />
						</div>";
				exit;
	}
}
else if(isset($_GET['decline']))
{
	$sql = "UPDATE user_friends SET friend_status='declined', friend_time='" . time() . "' WHERE friend_id='" . $_GET['decline'] . "'";
	if(!$result = $db->sql_query($sql))
	{
		printError("SQL ERROR", mysql_error() ."<br />" . mysql_errno());
	}
	else
	{		
		echo"<br /><br /><br /><br /><br /><br /><br /><br /><br />
				<center><meta http-equiv=\"refresh\"content=\"2;url=index.php?action=profile&amp;sub=friends\">";
				echo "<br/><h2>" . ucfirst(translate('friend_declined')) . "</h2>\n";
				echo "<br/><br />" . translate('redirectedin'). " <a href=\"index.php?action=profile&amp;sub=friends\">" . translate('clickhere') . "</a>.</center><div class='fixed'>
							</div>
						</div>
						</div>
					<div class='fixed'>
					</div>
					<div id='footer'>
					<a id='gotop' href='#' onclick='MGJS.goTop();return false;'>" . translate('top') . "</a>
						<div align='center' id='copyright'>&copy; " . translate('copyrightyear') . " " . translate('header_text'). " - " .  translate('curversion') ." - <a href='index.php?action=contact'>" . translate('contact') ."</a> - <a href='index.php?action=about'>" . translate('about') . "</a> - " . translate('onlineusers'). ": " . getActiveUsers() . "<br />
						</div>";
				exit;
	}
}
/**else if(isset($_GET['request']))
{
	$sql = "INSERT INTO user_friends SET ".
	"user_id='" . $USER->getId() . "', ".
	"friend_user_id='" . $_GET['request'] . "', ".
	"friend_status='pending'";
	if(!$result = $db->sql_query($sql))
	{
		printerror("SQL ERROR", mysql_error() ."<br />" . mysql_errno());
	}
	else
	{
		$friend_status = translate('friend_request_sendt');
	}
}**/

if(isset($_REQUEST['id']))
	{
		$USER_PROFILE = new User('id', $_REQUEST['id']);
		$page_url = "index.php?action=profile&amp;id=" . $_REQUEST['id'];
	}
	else
	{
		$USER_PROFILE = $USER;
		$page_url = "index.php?action=profile";
		
	}

$friends_list = fetch_friends($USER->getId());
$f_list = array('friend' => array(), 'pending' => array());


foreach($friends_list as $k => $v)
{
	$online_time = is_user_online($v['user_id']);
	if($v['status'] == 'pending')
	{
		$f_list['pending'][] = $friends_list[$k];
	}
	elseif($v['status'] != 'declined')
	{
		$array = array('online_time' => $online_time['last_visit'], 'page' => $online_time['page']);
		$array = array_merge($friends_list[$k], $array);
		$f_list['friend'][] = $array;
	}
}

?>
<div id='postpath'>
		<a title="<?php echo translate('gotofrontpage'); ?>" href="index.php"><?php echo translate('home'); ?></a>
        &gt; <a href="index.php?action=blogs" title='<?php echo translate('showallblogs'); ?>'><?php echo translate('blog'); ?></a> 
		&gt; <a href="index.php?action=profile&amp;id=<?php echo $USER->getId(); ?>"><?php echo ucf($USER->getFirstname()) . " " . ucf($USER->getLastname()); ?></a>
        &gt; <a href="index.php?action=profile&amp;sub=friends" title='<?php echo translate('friends'); ?>'><?php echo translate('friends'); ?></a> 
</div>
<br />
<h1><img src="./img/people.png" alt="<?php echo translate('friends') . " (" . count($f_list['friend']) . ")"; ?>" /> <?php echo translate('friends') . " (" . count($f_list['friend']) . ")"; ?></h1>
<br />
<?php echo translate('friendsinfo'); ?>
<br /><br />
<?php 

echo "<table border='0'>";

foreach($f_list['friend'] as $k => $v)
{
	$online_time = $v['online_time'];
	$display_page = "";
	$fullname = $v['firstname'] . " " . $v['lastname'];
	if($online_time > 0)
	{
		$status = "online";
		$status_text = str_replace('VAR1', date("H:i:s", $online_time), translate('last_action'));
		if($v['page'] != "")
		{
			$display_page = " - " . $v['page'];
		}
	}
	else
	{
		$status = "offline";
		$status_text = translate('offline');
	}
	echo "<tr><td>";
			
			if(selectAvatarPath($v['user_id']) != "")
			{
			$profile_pic_path = $CONFIG['user_data_path'] . $v['user_id'] . "/thumbs_" . selectAvatarPath($v['user_id']);
			if(file_exists($profile_pic_path))
			{
				echo "<a href='index.php?action=profile&amp;id=" . $v['user_id'] . "'><img src=\"" . $profile_pic_path . "\" alt=\"" . translate('profile_picture') . "\" /></a>\n";
			}
				else
				{
					echo $profile_pic_path;
				}
			}
			else
			{
				if(selectAvatarPath($v['user_id']) == "female")
				{
					echo "<center><a href='index.php?action=profile&amp;id=" . $v['user_id'] . "'><img src=\"" . $CONFIG['user_data_path'] . "profile_female.gif\" height='60' width='60' alt=\"" . translate('profile_picture') . "\" /></a></center>\n";
				}
				else
				{
					echo "<center><a href='index.php?action=profile&amp;id=" . $v['user_id'] . "'><img src=\"" . $CONFIG['user_data_path'] . "profile_male.gif\" height='60' width='60' alt=\"" . translate('profile_picture') . "\" /></a></center>\n";
				}
			}
			
			echo "</td><td><img src=\"img/" . $status . ".png\" alt=\"" . translate('friend_offline') . "\" /> <a href=\"index.php?action=profile&amp;id=" . $v['user_id'] . "\" title=\"" . $status_text . "\" class=\"" . $status . "_user\"><b>" . $fullname . "</b></a> " . $display_page . "</td></tr>";
}
echo "</table><br />";

	if(count($f_list['pending']) >= 1)
	{
	
	echo "<h3>" . translate('pending_friends') . " (" . count($f_list['pending']) . ")</h3>" . "<br />\n";
	
	echo "<table border='0'>";
	
	foreach($f_list['pending'] as $k => $v)
	{
		$online_time = is_user_online($v['user_id']);
		$fullname = $v['firstname'] . " " . $v['lastname'];
		
		echo "<tr><td>";
				
				if(selectAvatarPath($v['user_id']) != "")
				{
				$profile_pic_path = $CONFIG['user_data_path'] . $v['user_id'] . "/thumbs_" . selectAvatarPath($v['user_id']);
				if(file_exists($profile_pic_path))
				{
					echo "<a href='index.php?action=profile&amp;id=" . $v['user_id'] . "'><img src=\"" . $profile_pic_path . "\" alt=\"" . translate('profile_picture') . "\" /></a>\n";
				}
					else
					{
						echo $profile_pic_path;
					}
				}
				else
				{
					if(selectAvatarPath($v['user_id']) == "female")
					{
						echo "<center><a href='index.php?action=profile&amp;id=" . $v['user_id'] . "'><img src=\"" . $CONFIG['user_data_path'] . "profile_female.gif\" height='60' width='60' alt=\"" . translate('profile_picture') . "\" /></a></center>\n";
					}
					else
					{
						echo "<center><a href='index.php?action=profile&amp;id=" . $v['user_id'] . "'><img src=\"" . $CONFIG['user_data_path'] . "profile_male.gif\" height='60' width='60' alt=\"" . translate('profile_picture') . "\" /></a></center>\n";
					}
				}
		
				echo "</td><td><img src=\"img/add.png\" alt=\"" . translate('pending_friend_request') . "\"/> <a href=\"index.php?action=profile&amp;id=" . $v['user_id'] . "\" title=\"" . translate('pending_friend_request') . "\" class=\"pending_user\">" . $fullname . "</a></td>";
				
				if(checkSession() && isset($USER))
				{
					if($v['sender_id'] != $USER->getId())
					{
						echo "<td><a href=\"index.php?action=profile&amp;sub=friends&amp;accept=" . $v['id'] . "\" title=\"" . translate('accept_friend_long') . "\"> <img src=\"img/accept.png\" alt=\"" . translate('accept_friend') . "\" /></a> <a href=\"index.php?action=profile&amp;sub=friends&amp;decline=" . $v['id'] . "\" title=\"" . translate('decline_friend_long') . "\"><img src=\"img/decline.png\" alt=\"" . translate('decline_friend') . "\" /></a></td></tr>";
					}
				}
	}
	echo "</table><br />";
}
?>
